package com.nopay.dxdwphone.utils;

import com.blankj.utilcode.util.SPUtils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.nopay.dxdwphone.Constants;

/**
 * 会员管理工具
 * author：jun
 * date：2021/6/18
 */
public class MemberManager {

    private static final long TRIAL_LIMIT = 15 * 24 * 60 * 60 * 1000;

    public static boolean hasMemberTrialPermission() {
        int vipStatus = SPUtils.getInstance().getInt(Constants.SP_VIP_STATUS, 0);
        if(vipStatus == 0) {
            String createTime = SPUtils.getInstance().getString(Constants.SP_ACCOUNT_CREATE_TIME, "");
            if(!"".equals(createTime)) {
                long nowTime = new Date().getTime();
                long createTimeMillisecond = 0;
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date createTimeDate = format.parse(createTime);
                    createTimeMillisecond = createTimeDate.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return nowTime - createTimeMillisecond < TRIAL_LIMIT;
            }
        }
        return false;
    }

    public static boolean hasMemberPermission() {
        int vipStatus = SPUtils.getInstance().getInt(Constants.SP_VIP_STATUS, 0);
        if(vipStatus == 1 || vipStatus == 2) {
            String invalidTime = SPUtils.getInstance().getString(Constants.SP_VIP_INVALID_TIME, "");
            if(!"".equals(invalidTime)) {
                long nowTime = new Date().getTime();
                long vipTime = 0;
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                try {
                    Date vipDate = format.parse(invalidTime);
                    vipTime = vipDate.getTime();
                } catch (ParseException e) {
                    e.printStackTrace();
                }
                return vipTime > nowTime;
            }
        }
        return false;
    }

    private static int userTimes = 0;

    public static void setUserTimes(int userTimes) {
        MemberManager.userTimes = userTimes;
    }

    public static String getUserTimesKey() {
        String today = new SimpleDateFormat("yyyyMMdd").format(new Date());
        return Constants.SP_USER_TIMES + "_" + today;
    }

    public static void saveUserTimes(int userTimes) {
        if(hasMemberPermission()) return;

        MemberManager.userTimes = userTimes;
        SPUtils.getInstance().put(getUserTimesKey(), MemberManager.userTimes);
    }

    /**
     * 添加使用次数
     */
    public static void addUseTimes() {
        if(hasMemberPermission()) return;

        // TODO 非会员使用次数加1
        MemberManager.userTimes++;
        SPUtils.getInstance().put(getUserTimesKey(), MemberManager.userTimes);
    }

    /**
     * 是否有免费次数
     */
    public static boolean hasFreeTimes() {
        return MemberManager.userTimes < 10;
    }
}
